UP | HOME

目录

#+TITLE 判断是否支持Http keep-alive

方法一

没有开启Nginx Keepalive

curl -Iv https://beta.company/ --next https://beta.company/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to beta.company (127.0.0.1) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: beta.company
* Server certificate: TrustAsia DV SSL CA - G5
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> HEAD / HTTP/1.1
> Host: beta.company
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 302 Found
HTTP/1.1 302 Found
< Server: nginx/1.9.3
Server: nginx/1.9.3
< Date: Mon, 10 Apr 2017 04:57:35 GMT
Date: Mon, 10 Apr 2017 04:57:35 GMT
< Content-Length: 0
Content-Length: 0
< Connection: close
Connection: close
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Methods: POST, GET, OPTIONS
< Access-Control-Max-Age: 1000
Access-Control-Max-Age: 1000
< Set-Cookie: SESSION=3a52bf55-385e-46af-895d-b078d76c1145; Path=/; Secure; HttpOnly
Set-Cookie: SESSION=3a52bf55-385e-46af-895d-b078d76c1145; Path=/; Secure; HttpOnly
< Location: https://beta.company/login
Location: https://beta.company/login
< Content-Language: en-US
Content-Language: en-US

<
* Curl_http_done: called premature == 0
* Closing connection 0
* Hostname beta.company was found in DNS cache
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to beta.company (127.0.0.1) port 443 (#1)
* SSL re-using session ID
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: beta.company
* Server certificate: TrustAsia DV SSL CA - G5
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> HEAD / HTTP/1.1
> Host: beta.company
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 302 Found
< Server: nginx/1.9.3
< Date: Mon, 10 Apr 2017 04:57:36 GMT
< Content-Length: 0
< Connection: close
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, GET, OPTIONS
< Access-Control-Max-Age: 1000
< Set-Cookie: SESSION=950715f2-b8dc-4aad-a91e-68cc2b70aa98; Path=/; Secure; HttpOnly
< Location: https://beta.company/login
< Content-Language: en-US
<
* Curl_http_done: called premature == 0
* Closing connection 1

开启了Nginx Keepalive

curl -Iv https://beta.company/ --next https://beta.company/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to beta.company (127.0.0.1) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: beta.company
* Server certificate: TrustAsia DV SSL CA - G5
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> HEAD / HTTP/1.1
> Host: beta.company
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 302 Found
HTTP/1.1 302 Found
< Server: nginx/1.9.3
Server: nginx/1.9.3
< Date: Mon, 10 Apr 2017 05:06:42 GMT
Date: Mon, 10 Apr 2017 05:06:42 GMT
< Content-Length: 0
Content-Length: 0
< Connection: keep-alive
Connection: keep-alive
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Methods: POST, GET, OPTIONS
< Access-Control-Max-Age: 1000
Access-Control-Max-Age: 1000
< Set-Cookie: SESSION=9ddf4115-654a-40a1-8894-8975b334a5dc; Path=/; Secure; HttpOnly
Set-Cookie: SESSION=9ddf4115-654a-40a1-8894-8975b334a5dc; Path=/; Secure; HttpOnly
< Location: https://beta.company/login
Location: https://beta.company/login
< Content-Language: en-US
Content-Language: en-US

<
* Curl_http_done: called premature == 0
* Connection #0 to host beta.company left intact
* Found bundle for host beta.company: 0x7fe3cac0c7f0 [can pipeline]
* Re-using existing connection! (#0) with host beta.company
* Connected to beta.company (127.0.0.1) port 443 (#0)
> HEAD / HTTP/1.1
> Host: beta.company
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 302 Found
< Server: nginx/1.9.3
< Date: Mon, 10 Apr 2017 05:06:43 GMT
< Content-Length: 0
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, GET, OPTIONS
< Access-Control-Max-Age: 1000
< Set-Cookie: SESSION=9647d698-1779-4e09-8080-fcc7770e5701; Path=/; Secure; HttpOnly
< Location: https://beta.company/login
< Content-Language: en-US
<
* Curl_http_done: called premature == 0
* Connection #0 to host beta.company left intact

它们的区别:

diff /tmp/keep.txt /tmp/no-keep.txt

< * Connection #0 to host beta.company left intact
< * Found bundle for host beta.company: 0x7fe3cac0c7f0 [can pipeline]
< * Re-using existing connection! (#0) with host beta.company
< * Connected to beta.company (127.0.0.1) port 443 (#0)
---
> * Closing connection 0
> * Hostname beta.company was found in DNS cache
> *   Trying 127.0.0.1...
> * TCP_NODELAY set
> * Connected to beta.company (127.0.0.1) port 443 (#1)
> * SSL re-using session ID
> * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
> * Server certificate: beta.company
> * Server certificate: TrustAsia DV SSL CA - G5
> * Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
50c56
< < Date: Mon, 10 Apr 2017 05:06:43 GMT
---
> < Date: Mon, 10 Apr 2017 05:08:17 GMT
52c58
< < Connection: keep-alive
---
> < Connection: close

可以看到,开启了Keep alive的链接信息为 re-useing existing connection 而没有 keep alive 的话,则是 Connected to

方法二

直接查看 Chrome 浏览器的调试中的请求头和响应头

Request URL:https://beta.company/login
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:443
Referrer Policy:origin

Response Headers
Access-Control-Allow-Methods:POST, GET, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1000
Connection:close
Content-Encoding:gzip
Content-Language:en-US
Content-Type:text/html;charset=utf-8
Date:Mon, 10 Apr 2017 05:00:39 GMT
Server:nginx/1.9.3
Transfer-Encoding:chunked
Vary:Accept-Encoding


Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
Cache-Control:no-cache
Connection:keep-alive

可以看到,客户端浏览器发送了 Connection:keep-alive 的请求头,但服务器响应却是 Connection:close 这表示服务器端不支持 http keep-alive 。如果服务器端开启支持的话,它返回的是 Connection:keep-alive

关于 http keey-alive 与 tcp keeyalive 的资料

作者: emacsist

Created: 2017-04-12 Wed 22:53